package com.lsongh.springboot2.controller;

import com.lsongh.springboot2.entity.Activity;
import com.lsongh.springboot2.entity.News;
import com.lsongh.springboot2.entity.Result;
import com.lsongh.springboot2.service.ActivityService;
import com.lsongh.springboot2.service.UserService;
import com.lsongh.springboot2.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ActivityController {

    @Autowired
    ActivityService activityService;

    @Autowired
    UserService userService;

    /**
     * 获取所有的活动列表
     * @param name
     * @return
     */
    @GetMapping("activity/findAll")
    public Result<Activity> findAllAct(@RequestParam("name") String name){
        if (name == ""){
            List<Activity> list = activityService.findAllAct(0);
            if (list.size()>0){
                for (Activity activity:list){
                    activity.setActUserName(userService.findOne(activity.getActUserId()).getName());
                }
            }
            return ResultUtil.success(list);
        }else {
            List<Activity> list = activityService.findAllActByName(0,name);
            if (list.size()>0){
                for (Activity activity:list){
                    activity.setActUserName(userService.findOne(activity.getActUserId()).getName());
                }
            }
            return ResultUtil.success(list);
        }
    }

    /**
     * 删除一条活动
     * @param id
     * @return
     */
    @DeleteMapping("act/delete/{id}")
    public Result<String> deleteActivity(@PathVariable("id") int id){
        activityService.deleteActivityById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 添加活动
     * @param activity
     * @return
     */
    @PostMapping("act/add")
    public Result<Activity> addActivity(@RequestBody Activity activity){
        activityService.save(activity);
        return ResultUtil.success("添加成功");
    }

    /**
     * 修改活动
     * @param activity
     * @return
     */
    @PostMapping("act/update")
    public Result<String> updateActivity(@RequestBody Activity activity){
        activityService.save(activity);
        return ResultUtil.success("添加成功");
    }

    @GetMapping("act/{id}")
    public Result<Activity> findOneActivity(@PathVariable("id")int id){
        Activity activity = activityService.findOne(id);
        return ResultUtil.success(activity);
    }



}
